/*
 * wccli.c
 *
 *  Created on: Apr 1, 2012
 *      Author: root
 */
#include    "unp.h"

int
main(int argc, char **argv)
{
    int                    sockfd;
    struct sockaddr_in    servaddr;
    char    sendline[MAXLINE], recvline[MAXLINE];
    int str_len=0;

    if (argc != 2)
        err_quit("usage: tcpcli <IPaddress>");

    sockfd = Socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERV_PORT);
    Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);


    Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

    //str_cli(stdin, sockfd);        /* do it all */


     while (Fgets(sendline, MAXLINE, stdin) != NULL) {

            if(!strcmp(sendline,".\n"))
            {
            	printf("---------close client----------\n");
                Writen(sockfd,sendline, strlen(sendline)); // send to server "."

                // read all byte, all word, all line #
                if ((str_len=Readline(sockfd, recvline, MAXLINE)) == 0)
                    err_quit("str_cli: server terminated prematurely");


                Fputs(recvline, stdout);
                memset(recvline,0,strlen(recvline));
                break;
            }

            Writen(sockfd, sendline, strlen(sendline));

            if ((str_len=Readline(sockfd, recvline, MAXLINE)) == 0)
                err_quit("str_cli: server terminated prematurely");



            Fputs(recvline, stdout);
            memset(recvline,0,strlen(recvline)); // recvline initialization
            Fputs(recvline,stdout);
            memset(sendline,0,strlen(sendline)); // sendline initialization
            Fputs(sendline,stdout);

        }


    exit(0);
}

